package com.hl.util.KL.SocketClients;

import org.json.JSONArray;
import org.json.JSONException;

import android.os.Handler;
import android.os.Message;


class SendCallback implements Acknowledge {
	private Acknowledge targetAcknowledge;
	
	private boolean toMain;
	
	private static Handler handler = new Handler()
	{
		@Override
		public void handleMessage(android.os.Message msg) {
			JSONArray arr = (JSONArray)msg.obj;
			try {
				Acknowledge ack = (Acknowledge)arr.get(0);
				JSONArray args = (JSONArray)arr.get(1);
				ack.acknowledge(args);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	};
	
	SendCallback(Acknowledge ack, boolean performOnMain)
	{
		this.targetAcknowledge = ack;
		this.toMain = performOnMain;
	}
	@Override
	public void acknowledge(JSONArray arguments) {
		if (targetAcknowledge == null) {
			return;
		}
		if (toMain) {
			JSONArray args = new JSONArray();
			args.put(targetAcknowledge);
			args.put(arguments);
			Message sendMsg = new Message();
			sendMsg.obj = args;
			handler.sendMessage(sendMsg);
		}
		else {
			targetAcknowledge.acknowledge(arguments);
		}
	}

}
